#include "./cir-queue.h"
#include <iostream>

void initCircularQueue(CircularQueue &queue)
{
	queue.base = (ElemType *)malloc(MAX_SIZE * sizeof(ElemType));
	queue.front = queue.rear = 0;
}

int circularQueueEmpty(CircularQueue &queue)
{
	return queue.front == queue.rear;
}

int circularQueueFull(CircularQueue &queue)
{
	return (queue.rear + 1) % MAX_SIZE == queue.front;
}

int circularQueueLength(CircularQueue &queue)
{
	return (queue.rear - queue.front + MAX_SIZE) % MAX_SIZE;
}

void enterCircularQueue(CircularQueue &queue, ElemType e)
{
	// 队满则不进行操作
	if (circularQueueFull(queue))
	{
		return;
	}
	queue.base[queue.rear] = e;
	queue.rear = (queue.rear + 1) % MAX_SIZE;
}

ElemType goOutCircularQueue(CircularQueue &queue)
{
	if (circularQueueEmpty(queue))
	{
		return 0;
	}
	ElemType e = queue.base[queue.front];
	queue.front = (queue.front + 1) % MAX_SIZE;
	return e;
}

void circularQueueTraverse(CircularQueue &queue, void (*visit)(ElemType &e))
{
	int pointer = queue.front;
	while (pointer != queue.rear)
	{
		visit(queue.base[pointer]);
		pointer++;
	}
}